package ThreadDemo;

import java.util.Scanner;
//等待集（wait和notify的操作）


public class ThreadDemo10 {
    public static void main(String[] args) {
        Object locker=new Object();
        Thread thread1=new Thread(new Runnable() {
            @Override
            public void run() {
                //四处的对象应相同1，2，3，4
                synchronized (locker) {//1
                    while (true) {
                        System.out.println("线程1开始等待");
                        try {
                            locker.wait();//2
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("线程1停止等待");
                    }
                }
            }
        });
        thread1.start();
        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                Scanner scanner=new Scanner(System.in);
                System.out.println("请输入任意一个整数，继续执行notify");
                int num=scanner.nextInt();
                synchronized (locker){//3
                    System.out.println("线程2开始");
                    locker.notify();//4
                    System.out.println("线程2完毕");
                }
            }
        });
        thread2.start();
    }
}
